package com.zygk.locationtool.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import butterknife.BindView;
import butterknife.ButterKnife;
import com.zygk.library.base.BaseActivity;
import com.zygk.library.util.ListUtils;
import com.zygk.library.view.FixedListView;
import com.zygk.library.view.SearchView;
import com.zygk.locationtool.R;
import com.zygk.locationtool.adapter.StationSearchAdapter;
import com.zygk.locationtool.dao.StationLogic;
import com.zygk.locationtool.model.M_Station;
import com.zygk.locationtool.model.apimodel.APIM_StationList;
import com.zygk.locationtool.util.HttpRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Wikison on 2018/12/13.
 * 搜索站点
 */
public class StationSearchActivity extends BaseActivity {
  @BindView(R.id.searchView) SearchView searchView;
  @BindView(R.id.listView) FixedListView listView;
  @BindView(R.id.ll_no_data) LinearLayout llNoData;

  private StationSearchAdapter stationSearchAdapter;
  private List<M_Station> stationList = new ArrayList<>();

  @Override protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ButterKnife.bind(this);
  }

  @Override public void setContentView() {
    setContentView(R.layout.activity_station_search);
  }

  @Override public void init() {
    initData();
    initView();
    initListener();
  }

  private void initData() {
    stationSearchAdapter = new StationSearchAdapter(mContext, stationList);
    listView.setAdapter(stationSearchAdapter);
  }

  private void initView() {
    searchView.setTvCancelVisible(View.GONE);
    searchView.setBackVisible(View.VISIBLE);
    searchView.setBackgroundResource(R.drawable.drawable_bg_head);
    searchView.setStrHint("输入站点名称");
  }

  private void initListener() {
    stationSearchAdapter.setOnItemClickListener(new StationSearchAdapter.onItemClickListener() {
      @Override public void onItemClick(M_Station station) {
        Intent intent = new Intent(mContext, MapActivity.class);
        intent.putExtra(MapActivity.INTENT_STATION, station);
        startActivity(intent);
      }
    });

    searchView.setOnBackClickListner(new SearchView.OnBackClickListener() {
      @Override public void onBackClick() {
        finish();
      }
    });

    searchView.setOnThinkingClickListener(new SearchView.OnThinkingClickListener() {
      @Override public void onThinkingClick(String text) {
        searchStationList(text);
      }
    });

    searchView.setSearchViewListener(new SearchView.SearchViewListener() {
      @Override public void onSearch(String text) {

      }

      @Override public void onClear() {

      }
    });
  }

  private void searchStationList(String stationName) {
    StationLogic.getSearchStationList(mContext, stationName, new HttpRequest.HttpCallback() {
      @Override public void onStart() {

      }

      @Override public void onSucceed(Object obj) {
        APIM_StationList result= (APIM_StationList)obj;
        fillAdapter(result.getResultObj());
      }

      @Override public void onFailed() {

      }

      @Override public void onFinish() {

      }
    });
  }

  // 填充数据
  private void fillAdapter(List<M_Station> list) {
    if (ListUtils.isEmpty(list)) {
      llNoData.setVisibility(View.VISIBLE);
      listView.setVisibility(View.GONE);
    } else {
      llNoData.setVisibility(View.GONE);
      listView.setVisibility(View.VISIBLE);
      stationSearchAdapter.setData(list);
    }
  }
}
